import { Equal, LessThan, Like, MoreThan } from 'typeorm';

export const conditionWrapper = (value: string, condition: string) => {
  let cond = null;
  if (!condition) {
    cond = Equal(value);
  } else {
    const c1 = condition.toLocaleLowerCase();
    const conditionExpression = ['like', '=', '<', '>'];
    if (!conditionExpression.includes(c1)) {
      throw new Error('condition 只能是 like,<, >,=中的一个');
    }

    switch (c1) {
      case 'like':
        cond = Like('%' + value + '%');
        break;
      case '=':
        cond = Equal(value);
        break;
      case '>':
        cond = MoreThan(value);
        break;
      case '<':
        cond = LessThan('%' + value + '%');
        break;
      default:
        cond = Equal(value);
        break;
    }
  }
  return cond;
};
